//
//  A_addJLGJViewController.m
//  Estate
//
//  Created by heyong on 16/6/21.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "A_addJLGJViewController.h"

#import "QianDTableCell.h"
#import "selectBtnCell.h"
#import "PositionTableCell.h"
#import "C_wladdCell_1.h"
#import "B_wuLiaoCell.h"
#import "DefaineCell.h"
#import "ActionSheetDatePicker.h"
#import "OrientationViewController.h"
#import "TableTopStateCell.h"
#import "ProjectUserModel.h"

@interface A_addJLGJViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *testArr;
}


@property(strong, nonatomic)TPKeyboardAvoidingTableView *tableView;
// 保存按钮被点击
@property (assign, nonatomic) BOOL save;

//位置信息
@property (copy, nonatomic) NSString *positionwStr;


// 记录点击的施工单位下标
@property (assign, nonatomic) int unitIv;
// 施工单位数据
@property (strong, nonatomic) NSDictionary *unitDic;
// 整改人数据
@property (strong, nonatomic) NSArray *peopleArr;
// 位置 cell
@property (strong, nonatomic) PositionTableCell *positionCell;

// 创建时间
@property (copy, nonatomic) NSString *nowTime;
// 创建标题
@property (copy, nonatomic) NSString *titleStr;
// 内容
@property (copy, nonatomic) NSString *content;

// 问题类型
@property (assign, nonatomic) NSInteger problemType;
// 整改人ID
@property (copy, nonatomic) NSString *userID;
// 整改单位ID
@property (copy, nonatomic) NSString *unitID;
//整改单位名称
@property (copy, nonatomic) NSString *unitStr;
// 严重度
@property (copy, nonatomic) NSString *level;
// 时限
@property (copy, nonatomic) NSString *endTime;
// 发送时间
@property (copy, nonatomic) NSString *sendTime;
// 发送形式
@property (assign, nonatomic) NSInteger sendType;


//要求
@property (copy, nonatomic) NSString *processRequired;



// 选择的负责人(监理)ID
@property (copy, nonatomic) NSString *jlIDStr;
//负责人姓名
@property(copy ,nonatomic)NSString *jlStr;

// 记录生成的流程ID
@property (strong, nonatomic) NSString *processID;

@property(strong, nonatomic)NSMutableArray  *arr;

// 第三组行数
@property (assign, nonatomic) int rows;

// 存放监理数组
@property (strong, nonatomic) NSArray *JLArr;

// 监理ID数组
@property (strong, nonatomic) NSMutableArray *jlIDArr;

// block参数
@property (strong, nonatomic) PositionBlockModel *blockModel;
// 当前被点的地址数据模型
@property (strong, nonatomic) AddressModel *addressModel;
// addressID
@property (copy, nonatomic) NSString *addressID;
// 督办单无得形式
@property (strong, nonatomic) NSMutableArray *typeArr;
// 流程记录ID
@property (strong, nonatomic) NSString *flowRecordID;
// 附件信息
@property (strong, nonatomic) NSString *fileinfos;


@end

@implementation A_addJLGJViewController

// 发送类型
- (NSMutableArray *)typeArr {
    if (!_typeArr) {
        _typeArr = [[NSMutableArray alloc] init];
    }
    return _typeArr;
}

-(NSMutableArray *)arr{
    if (!_arr) {
        _arr = [NSMutableArray array];
    }
    return _arr;
}

- (TPKeyboardAvoidingTableView *)tableView {
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-104)style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.showsVerticalScrollIndicator=NO;
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"qCell"];
        [_tableView registerClass:[selectBtnCell class] forCellReuseIdentifier:@"sCell"];
        [_tableView registerClass:[PositionTableCell class] forCellReuseIdentifier:@"pCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"C_wladdCell_1" bundle:nil] forCellReuseIdentifier:@"doubleCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"defaine"];
        [_tableView registerNib:[UINib nibWithNibName:@"TableTopStateCell" bundle:nil] forCellReuseIdentifier:@"firstCell"];
    }
    return _tableView;
}

- (NSDictionary *)unitDic {
    if (!_unitDic) {
        _unitDic = [[NSDictionary alloc] init];
    }
    return _unitDic;
}

- (NSArray *)peopleArr {
    if (!_peopleArr) {
        _peopleArr = [[NSArray alloc] init];
    }
    return _peopleArr;
}

- (NSMutableArray *)jlIDArr {
    if (!_jlIDArr) {
        _jlIDArr = [[NSMutableArray alloc] init];
    }
    return _jlIDArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    if (self.addGJ) {
        testArr=[[NSArray alloc]initWithObjects:@"工作联系单",@"监理通知单",@"工作暂停令", nil];
        
    } else if (self.addDuB) {
        testArr=[[NSArray alloc]initWithObjects:@"工作联系单",@"监理通知单",@"工作暂停令", @"无", nil];
    }
    
    self.rows = 5;
    self.JLArr = [self getJLDataSource];
    //默认巡视
    self.problemType=1;
    //编辑跟进
    if(self.edit){
        //查询数据
        [self loadData];
    }
    
    if (self.addDuB && self.sendType == SEND_TYPE_NULL) {
        self.rows = 3;
        [self.tableView reloadData];
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chuanzhi:) name:@"chuanzhi" object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


// 获取监理数据
- (NSArray *)getJLDataSource {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *proID = DEF_PERSISTENT_GET_OBJECT(@"proid");
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM PROJECT_USER WHERE PRO_ID = '%@' AND USER_TYPE = 'USER_03'", proID];
    FMResultSet *result = [db executeQuery:sql];
    // 模型数组
    NSMutableArray *modelArr = [NSMutableArray array];
    // 监理数组
    NSMutableArray *jlArr = [NSMutableArray array];
    while ([result next]) {
        ProjectUserModel *model = [[ProjectUserModel alloc] init];
        
        model.ID = [result stringForColumn:@"ID"];
        model.PRO_ID = [result stringForColumn:@"PRO_ID"];
        model.UNIT_ID = [result stringForColumn:@"UNIT_ID"];
        model.USER_ID = [result stringForColumn:@"USER_ID"];
        model.USER_TYPE = [result stringForColumn:@"USER_TYPE"];
        model.PRO_USER_DESC = [result stringForColumn:@"PRO_USER_DESC"];
        model.CREATE_USER_NAME = [result stringForColumn:@"CREATE_USER_NAME"];
        model.CREATE_TIME = [result stringForColumn:@"CREATE_TIME"];
        model.CREATE_IP = [result stringForColumn:@"CREATE_IP"];
        model.MODIFY_USER_NAME = [result stringForColumn:@"MODIFY_USER_NAME"];
        model.MODIFY_IP = [result stringForColumn:@"MODIFY_IP"];
        model.MODIFY_TIME = [result stringForColumn:@"MODIFY_TIME"];
        model.TITLE_TYPE = [result stringForColumn:@"TITLE_TYPE"];
        model.TITLE_NAME = [result stringForColumn:@"TITLE_NAME"];
        model.EXPIRE_TIME = [result stringForColumn:@"EXPIRE_TIME"];
        model.IS_CHECK = [result stringForColumn:@"IS_CHECK"];
        
        [modelArr addObject:model];
        [self.jlIDArr addObject:model.USER_ID];
        NSString *JLName = [LSUtility getuserIdByUserName:model.USER_ID][@"REAL_NAME"];
        
        if (JLName) {
            [jlArr addObject:JLName];
        }
    }
    [db close];
    return jlArr;
}

#pragma mark -编辑查询数据
-(void)loadData{
    //位置信息
    self.positionwStr =  self.recordModel.ADDRES_INFO;
    //位置id
    self.addressID = self.recordModel.ADDRESS_ID;
    //发送形式
    self.sendType=self.recordModel.SEND_TYPE;
    //巡视 安全 旁站类型
    self.problemType=self.recordModel.PROBLEM_TYPE;
    
    //整改单位
    self.unitID=self.recordModel.RECIFY_UNIT;
    
    //整改人
    self.userID=self.recordModel.RECIFY_USER;
    self.peopleArr = [self getPeopleData:self.unitID];
    
    //整改单位名称
    NSDictionary *dic=[LSUtility getUnitNameByUnitId: self.unitID];
    self.unitStr=DEFISNUL(dic[@"PRO_UNIT_NAME"]) ;
    
    //负责人id
    self.jlIDStr=self.recordModel.RECORD_MASTER;
    DeLog(@"负责人id:%@", self.jlIDStr);
    //负责人姓名
    NSDictionary *dicJl= [LSUtility getuserIdByUserName: self.jlIDStr];
    self.jlStr=dicJl[@"REAL_NAME"];
    
    //时限
    NSDictionary *dicSX=[self queryProcessByProcessID:self.superProcessId];
    self.endTime=DEFISNUL(dicSX[@"REQUIRED_END_TIME"]);
    
    //严重度
    self.level=self.recordModel.SEVERITY;
    //发起时间
    self.sendTime=self.recordModel.SEND_TIME;
    // 标题
    self.processTitle=self.recordModel.PROCESS_TITLE;
    //描述
    self.processContent=self.recordModel.PROCESS_CONTENT;
    //要求
    self.processRequired=self.recordModel.PROCESS_REQUIRED;
    //附件
    self.fileinfos=self.recordModel.FILEINFOS;
    
    //刷新数据源
    [self.tableView reloadData];
}

//通过recoedId得到时限process时限
#pragma mark -通过流程id得到流程记录信息
-(NSDictionary *)queryProcessByProcessID:(NSString *)processId{
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sqlStr=[NSString stringWithFormat:@"select * from process where id='%@'",processId];
    FMResultSet *result = [db executeQuery:sqlStr];
    NSDictionary *dic=[NSDictionary dictionary];
    while ([result next]) {
        dic=[result resultDictionary];
    }
    [db close];
    return dic;
}

#pragma  mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0.001;
    }else{
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 7;
    }else if (section==1){
        return 3;
    }else{
        return self.rows;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        if(row == 0){
            return 30;
        }
        return 44;
    }else if(section==1){
        if(row==0){
            return 44;
        }else{
            return 100;
        }
    }else{
        return 44;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //组
    long section=indexPath.section;
    //行
    long row=indexPath.row;
    if (section==0) {
        if (row == 0) {
            TableTopStateCell * celld = [tableView dequeueReusableCellWithIdentifier:@"firstCell" forIndexPath:indexPath];
            celld.backgroundColor = [UIColor colorWithRed:0.7 green:0.97 blue:0.97 alpha:1.00];
            for (id obj in celld.contentView.subviews) {
                [obj removeFromSuperview];
            }
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((DEF_SCREEN_WIDTH - 100)/2, 0, 100, 30)];
            [celld.contentView addSubview:btn];
            
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 14, 14)];
            [btn addSubview:imgView];
            imgView.image = [UIImage imageNamed:@"chulizhong"];
            
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 70, 30)];
            [btn addSubview:lab];
            [lab setFont:[UIFont systemFontOfSize:12]];
            
            if (self.recordModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW) {
                [lab setText:@"状态:待审阅"];
                
            } else if (self.recordModel.PROCESS_STAGE == PROCESS_STAGE_REVIEW_PASS_NO) {
                [lab setText:@"状态:审阅不通过"];
                
            } else if (self.recordModel.PROCESS_STAGE == PROCESS_STAGE_CHECK) {
                [lab setText:@"状态:待复查"];
                
            } else if (self.recordModel.PROCESS_STAGE == PROCESS_STAGE_CHECK_PASS_YES) {
                [lab setText:@"状态:复查通过"];
                
            } else if (self.recordModel.PROCESS_STAGE == PROCESS_STAGE_END) {
                [lab setText:@"状态:结束"];
                
            } else {
                [lab setText:@"状态:待处理"];
            }
            return celld;
            
        } else if (row==1) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.textField.placeholder=@"";
            cell.title.text=@"项  目:";
            if (self.addGJ || self.addDuB) {
                cell.isedit = NO;
                cell.textField.text = DEF_PERSISTENT_GET_OBJECT(@"proname");
            }
            return cell;
            
        } else if (row==2) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.textField.placeholder=@"";
            cell.title.text=@"时  间:";
            if (self.addGJ || self.addDuB) {
                cell.isedit = NO;
                self.nowTime = [LSUtility nowTime];
                cell.textField.text = self.nowTime;
            }
            
            return cell;
        } else if (row==3) {
            selectBtnCell *cell=[tableView dequeueReusableCellWithIdentifier:@"sCell" forIndexPath:indexPath];
            cell.clickXsBlock=^{
                self.problemType = PROBLEM_TYPE_SCOUT;
            };
            cell.clickAqBlock=^{
                self.problemType = PROBLEM_TYPE_SAFE;
            };
            cell.clickqtBlock=^{
                self.problemType = PROBLEM_TYPE_OTHER;
            };
            cell.clickPzBlock = ^ {
                self.problemType = PROBLEM_TYPE_SITE;
            };
            
            //设置选中项
            [cell setBtntype:[NSString stringWithFormat:@"%i",self.problemType]];
            cell.isEdit = YES;
            return cell;
            
        } else if(row==4) {
            PositionTableCell *cell =[tableView dequeueReusableCellWithIdentifier:@"pCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.isEdit=YES;
            if(self.positionwStr&&self.positionwStr.length>0){
                NSDictionary *dic = [LSUtility dictionaryWithJsonString:self.positionwStr];
                cell.btnStr = [NSString stringWithFormat:@"%@ %@",dic[@"address"],dic[@"leftDistanceStr"]];
            }else{
                cell.btnStr = (self.blockModel.keyStr ? self.blockModel.keyStr : @"请选择位置");
            }
            self.positionCell = cell;
            __weak PositionTableCell *weakCell = cell;
            cell.cliclWzBlock = ^{
                OrientationViewController *location=[[OrientationViewController alloc]init];
                location.flagStr = @"jilu";
                
                location.dataArr1 = [NSMutableArray arrayWithArray:self.blockModel.dataArr1];
                location.dataArr2 = [NSMutableArray arrayWithArray:self.blockModel.dataArr2];
                location.dataArr3 = [NSMutableArray arrayWithArray:self.blockModel.dataArr3];
                location.dataArr4 = [NSMutableArray arrayWithArray:self.blockModel.dataArr4];
                location.aID = self.blockModel.aID;
                location.firstID = self.blockModel.firstID;
                location.secondID = self.blockModel.secondID;
                location.thirdID = self.blockModel.thirdID;
                
                location.keyStr = self.blockModel.keyStr;
                location.number = self.blockModel.number;
                location.addressArr = [NSMutableArray arrayWithArray:self.blockModel.addressArr];
                location.position = self.blockModel.position;
                location.addressModel = self.addressModel;
                location.selectedX = self.blockModel.selectedX;
                location.selectedY = self.blockModel.selectedY;
                location.scale = self.blockModel.scale;
                
                if (self.positionwStr) {
                    NSDictionary *dic=[LSUtility dictionaryWithJsonString:  self.positionwStr];
                    CGFloat x=[dic[@"long"] floatValue];
                    CGFloat y=[dic[@"lat"] floatValue];
                    location.selectedX=x;
                    location.selectedY=y;
                }
                
                if(self.addressID){
                    location.number=2;
                }
                
                if (location.number) {
                    location.hC = YES;
                    //                    location.firstID=@"";
                    location.aID=self.addressID;
                }
                [self.navigationController pushViewController:location animated:YES];
                // 回传位置值
                location.returnPlaceInfoBlock = ^(PositionBlockModel *model, AddressModel *addModel, NSString *addInfoStr){
                    self.blockModel = model;
                    self.addressModel = addModel;
                    self.positionwStr = addInfoStr;
                    NSDictionary *dic = [LSUtility dictionaryWithJsonString:self.positionwStr];
                    weakCell.btnStr = [NSString stringWithFormat:@"%@ %@",dic[@"address"],dic[@"leftDistanceStr"]];
                    self.addressID = model.aID;
                };
            };
            return cell;
            
        } else if (row==5) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
            cell.title.text=@"发送形式:";
            
            if (self.sendType == 0) {
                cell.text.text = @"请选择";
            } else {
                cell.text.text = testArr[self.sendType - 1];
            }
            return cell;
            
        } else {
            C_wladdCell_1 *cell=[tableView dequeueReusableCellWithIdentifier:@"doubleCell" forIndexPath:indexPath];
            cell.text1.userInteractionEnabled = NO;
            cell.text2.userInteractionEnabled = NO;
            cell.duban = YES;
            cell.line.hidden=YES;
            cell.leftConstraint.constant = 0.;
            cell.title1.text=@"记录人:";
            cell.title2.text=@"负责人:";
            
            // 跟进单
            if (self.addGJ) {
                // 记录人
                if (self.recordModel.PROCESS_ID && ![self.recordModel.PROCESS_ID isEqualToString:@""]) {
                    NSDictionary *dic=[self queryProcessByProcessID:self.recordModel.PROCESS_ID];
                    cell.text1.text = [LSUtility getuserIdByUserName:dic[@"PROCESS_PUBLISHERS"]][@"REAL_NAME"];
                } else {
                    cell.text1.text = DEF_PERSISTENT_GET_OBJECT(@"realname");
                }
                // 负责人
                if (self.recordModel.RECORD_MASTER && ![self.recordModel.RECORD_MASTER isEqualToString:@""]) {
                    cell.text2.text = [LSUtility getuserIdByUserName:self.recordModel.RECORD_MASTER][@"REAL_NAME"];
                    
                } else {
                    cell.text2.text = DEF_PERSISTENT_GET_OBJECT(@"realname");
                }
            } else if (self.addDuB) {
                cell.text1.text = DEF_PERSISTENT_GET_OBJECT(@"realname");
                cell.text2.text = self.jlStr?self.jlStr:@"请选择";
            }
            return cell;
        }
    } else if (section==1) {
        WEAKSELF
        if (row==0) {
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.textField.text = DEFISNUL(self.processTitle);
            cell.title.text=@"标  题:";
            cell.textField.placeholder = @"请输入标题";
            cell.textValueChangedBlock = ^(NSString *valueStr){
                weakSelf.processTitle = valueStr;
            };
            
            return cell;
        } else if(row==1) {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.text.text=DEFISNUL(self.processContent);
            cell.title.text=@"描  述:";
            cell.textValueChangedBlock = ^(NSString *valueStr){
                weakSelf.processContent = valueStr;
            };
            
            return cell;
        } else {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.text.text=DEFISNUL(self.processRequired);
            cell.title.text=@"要  求:";
            cell.textValueChangedBlock = ^(NSString *valueStr){
                weakSelf.processRequired = valueStr;
            };
            return cell;
        }
    } else {
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"defaine" forIndexPath:indexPath];
        cell.line.hidden=YES;
        if(row==0){
            cell.title.text=@"时  限:";
            cell.text.text = self.endTime ? self.endTime : @"请选择";
            return cell;
        }else if(row==1){
            cell.title.text=@"严重度:";
            cell.text.text = self.level ? self.level : @"请选择";
            return cell;
        }else if (row==2){
            cell.title.text=@"发送时间:";
            cell.text.text= self.sendTime&&self.sendTime.length>0? self.sendTime:@"请选择";
            self.sendTime =DEFISNUL(self.sendTime) ;
            return cell;
        }else if(row==3){
            cell.title.text=@"负责单位:";
            NSArray *unitArr = self.unitDic[@"nameArr"];
            if(self.edit){
                cell.text.text= self.unitStr;
            }else{
                NSString *str=unitArr[self.unitIv] ? unitArr[self.unitIv] : @"请选择";
                cell.text.text =str;
                self.unitStr= str;
            }
            return cell;
        }else{
            cell.title.text=@"整改人:";
            NSString *changeStr;
            if (self.userID) {
                changeStr = [LSUtility getuserIdByUserName:self.userID][@"REAL_NAME"];
            }
            cell.text.text = changeStr ? changeStr : @"请选择";
            return cell;
        }
    }
}

/*
 *点击方法
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    long section=indexPath.section;
    long row=indexPath.row;
    //发送形式
    if(section == 0){
        if (row == 5) {
            NSNumber *ninit = [NSNumber numberWithInteger:0];
            [ActionSheetStringPicker showPickerWithTitle:nil rows:@[testArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                
                //得到选择下标
                int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                if (iv == 0) {
                    self.sendType = SEND_TYPE_CONTACTLIST;
                    [self.typeArr addObject:[NSString stringWithFormat:@"%d", SEND_TYPE_CONTACTLIST]];
                } else if (iv == 1) {
                    self.sendType = SEND_TYPE_SUPERVISIONNOTICE;
                    [self.typeArr addObject:[NSString stringWithFormat:@"%d", SEND_TYPE_SUPERVISIONNOTICE]];
                } else if (iv == 2) {
                    self.sendType = SEND_TYPE_STOPLIST;
                    [self.typeArr addObject:[NSString stringWithFormat:@"%d", SEND_TYPE_STOPLIST]];
                } else if (iv == 3) {
                    self.sendType = SEND_TYPE_NULL;
                    [self.typeArr addObject:[NSString stringWithFormat:@"%d", SEND_TYPE_NULL]];
                }
                // 获得上一次的选择
                int lastType = 0;
                if (self.typeArr.count > 1) {
                    lastType = [[self.typeArr firstObject] intValue];
                    [self.typeArr removeObjectAtIndex:0];
                }
                
                if (self.addDuB && lastType != SEND_TYPE_NULL && self.sendType == SEND_TYPE_NULL) {
                    self.rows = 3;
                    //将整改单位和整改人置为空
                    self.unitID = @"";
                    self.userID  = @"";
                    self.unitDic = nil;
                    self.unitStr = @"";
                    [weakSelf.tableView reloadData];
                } else if (self.addDuB && lastType == SEND_TYPE_NULL && self.sendType != SEND_TYPE_NULL) {
                    self.rows = 5;
                    [weakSelf.tableView reloadData];
                }
                
                DefaineCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                cell.text.text = testArr[iv];
            } cancelBlock:nil origin:self.view];
            
        } else if (self.addDuB && row == 6) {   // 负责人(监理)
            if (self.JLArr.count > 0) {
                NSNumber *ninit = [NSNumber numberWithInteger:0];
                [ActionSheetStringPicker showPickerWithTitle:nil rows:@[self.JLArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                    //得到选择下标
                    int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                    C_wladdCell_1 *cell = [tableView cellForRowAtIndexPath:indexPath];
                    cell.text2.text = self.JLArr[iv];
                    self.jlIDStr = self.jlIDArr[iv];
                    self.jlStr=self.JLArr[iv];
                } cancelBlock:nil origin:self.view];
                
            } else {
                [LSUtility showToastWithMessage:@"无可选择项" inView:self.navigationController.view Andtime:1.];
            }
        }
    } if(section==2){
        if(row==0){//时限
            NSDate *curDate = [NSDate date];
            ActionSheetDatePicker *picker = [[ActionSheetDatePicker alloc] initWithTitle:nil datePickerMode:UIDatePickerModeDateAndTime selectedDate:curDate doneBlock:^(ActionSheetDatePicker *picker, NSDate *selectedDate, id origin) {
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *strDate = [dateFormatter stringFromDate:selectedDate];
                DefaineCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                cell.text.text = strDate;
                self.endTime = strDate;
                //                [weakSelf.tableView reloadData];
                
            } cancelBlock:^(ActionSheetDatePicker *picker) {
                NSLog(@"%@", picker.description);
            } origin:self.view];
            
            picker.minimumDate = [NSDate date];
            [picker showActionSheetPicker];
            
        } else if (row == 2) {  // 发送时间
            NSDate *curDate = [NSDate date];
            ActionSheetDatePicker *picker = [[ActionSheetDatePicker alloc] initWithTitle:nil datePickerMode:UIDatePickerModeDateAndTime selectedDate:curDate doneBlock:^(ActionSheetDatePicker *picker, NSDate *selectedDate, id origin) {
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *strDate = [dateFormatter stringFromDate:selectedDate];
                DefaineCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                cell.text.text = strDate;
                self.sendTime = strDate;
                
            } cancelBlock:^(ActionSheetDatePicker *picker) {
                NSLog(@"%@", picker.description);
            } origin:self.view];
            
            picker.minimumDate = [NSDate date];
            [picker showActionSheetPicker];
        } else if (row==3){      // 负责单位
            // 获取施工单位数据
            self.unitDic = [self getUnitData];
            NSMutableArray *unitArr = self.unitDic[@"nameArr"];
            
            //查找监理单位
            NSDictionary *jianlidanwei = [LSUtility getjianliDanwei];
            [unitArr addObject: jianlidanwei[@"name"]];
            
            if (unitArr.count > 0) {
                NSNumber *ninit = [NSNumber numberWithInteger:0];
                [ActionSheetStringPicker showPickerWithTitle:nil rows:@[unitArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                    //得到选择下标
                    int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                    NSLog(@"%@",unitArr[iv]);
                    self.unitIv = iv;
                    DefaineCell *cell=[tableView cellForRowAtIndexPath:indexPath];
                    cell.text.text = unitArr[iv];
                    
                    // 获取整改人数据
                    if (iv == unitArr.count-1) {
                        self.unitID = jianlidanwei[@"id"];
                        self.userID = [self.jlIDArr firstObject];
                    }else{
                        NSArray *IDArr = self.unitDic[@"IDArr"];
                        self.unitID = IDArr[iv];
                        self.peopleArr = [self getPeopleData:self.unitID];
                        self.userID = [self.peopleArr firstObject];
                        
                    }
                    
                    // 刷新整改人cell
                    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:4 inSection:2];
                    [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
                } cancelBlock:nil origin:self.view];
                
            } else {
                [LSUtility showToastWithMessage:@"无可选择项" inView:self.navigationController.view Andtime:1.];
            }
            
        } else if (row==4){//整改人
            
            if (!self.unitID) {
                [LSUtility showToastWithMessage:@"请先选择整改单位" inView:self.view Andtime:1.];
                
            } else {
                NSDictionary *jianlidanwei = [LSUtility getjianliDanwei];
                if([self.unitID isEqualToString:jianlidanwei[@"id"]]){
                    if (self.JLArr.count > 0) {
                        NSNumber *ninit = [NSNumber numberWithInteger:0];
                        [ActionSheetStringPicker showPickerWithTitle:nil rows:@[self.JLArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                            //得到选择下标
                            int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                            DefaineCell *cell=[tableView cellForRowAtIndexPath:indexPath];
                            self.userID = self.jlIDArr[iv];
                            cell.text.text = self.JLArr[iv];
                        } cancelBlock:nil origin:self.view];
                        
                    } else {
                        [LSUtility showToastWithMessage:@"无可选择项" inView:self.navigationController.view Andtime:1.];
                    }
                }else{
                    NSNumber *ninit = [NSNumber numberWithInteger:0];
                    NSMutableArray *nameArr = [NSMutableArray array];
                    for (int i = 0; i < self.peopleArr.count; i++) {
                        NSString *name=[LSUtility getuserIdByUserName:self.peopleArr[i]][@"REAL_NAME"];
                        if(name){
                            [nameArr addObject:name];
                        }
                    }
                    if (nameArr.count > 0) {
                        [ActionSheetStringPicker showPickerWithTitle:nil rows:@[nameArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                            //得到选择下标
                            int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                            DefaineCell *cell=[tableView cellForRowAtIndexPath:indexPath];
                            NSString *userID = self.peopleArr[iv];
                            self.userID = userID;
                            cell.text.text = [LSUtility getuserIdByUserName:userID][@"REAL_NAME"];
                            //                    [weakSelf.tableView reloadData];
                        } cancelBlock:nil origin:self.view];
                        
                    } else {
                        [LSUtility showToastWithMessage:@"无可选择项" inView:self.navigationController.view Andtime:1.];
                    }
                }
                
            }
        } else if (row == 1){   // 严重度
            NSNumber *ninit = [NSNumber numberWithInteger:0];
            NSArray *arr=@[@"一般",@"严重"];
            [ActionSheetStringPicker showPickerWithTitle:nil rows:@[arr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                //得到选择下标
                int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                DefaineCell *cell=[tableView cellForRowAtIndexPath:indexPath];
                cell.text.text = arr[iv];
                
                self.level = arr[iv];
                //                [weakSelf.tableView reloadData];
            } cancelBlock:nil origin:self.view];
        }
    }
}

/*
 *  此方法让cell横线置顶
 */
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

// 获取整改单位数据
- (NSMutableDictionary *)getUnitData {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *proID = DEF_PERSISTENT_GET_OBJECT(@"proid");
    NSString *sql = [NSString stringWithFormat:@"SELECT PRO_UNIT_NAME, ID FROM PROJECT_UNIT WHERE PRO_ID = '%@' AND PRO_UNIT_TYPE = 'UNIT_02'", proID];
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *unitNameArr = [NSMutableArray array];
    NSMutableArray *IDArr = [NSMutableArray array];
    NSMutableDictionary *unitDic = [NSMutableDictionary dictionary];
    while ([result next]) {
        NSString *unitName = [result stringForColumn:@"PRO_UNIT_NAME"];
        NSString *ID = [result stringForColumn:@"ID"];
        [unitNameArr addObject:unitName];
        [IDArr addObject:ID];
    }
    [db close];
    [unitDic setObject:unitNameArr forKey:@"nameArr"];
    [unitDic setObject:IDArr forKey:@"IDArr"];
    return unitDic;
}

// 获取整改人数据
- (NSMutableArray *)getPeopleData:(NSString *)ID {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sql = [NSString stringWithFormat:@"SELECT USER_ID FROM PROJECT_USER WHERE UNIT_ID = '%@' AND (USER_TYPE = 'CONSTRACT_01' OR USER_TYPE = 'CONSTRACT_02' OR USER_TYPE = 'CONSTRACT_03' OR USER_TYPE = 'CONSTRACT_04' OR USER_TYPE = 'CONSTRACT_05' OR USER_TYPE = 'CONSTRACT_06' OR USER_TYPE = 'CONSTRACT_07')", ID];
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *nameArr = [NSMutableArray array];
    while ([result next]) {
        NSString *userID = [result stringForColumn:@"USER_ID"];
        [nameArr addObject:userID];
    }
    [db close];
    nameArr =[nameArr valueForKeyPath:@"@distinctUnionOfObjects.self"];
    return nameArr;
}
// 获取整改监理人数据
- (NSMutableArray *)getPeopleData1:(NSString *)ID {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sql = [NSString stringWithFormat:@"SELECT USER_ID FROM PROJECT_USER WHERE UNIT_ID = '%@' AND (USER_TYPE = 'CONSTRACT_01' OR USER_TYPE = 'CONSTRACT_02' OR USER_TYPE = 'CONSTRACT_03' OR USER_TYPE = 'CONSTRACT_04' OR USER_TYPE = 'CONSTRACT_05' OR USER_TYPE = 'CONSTRACT_06' OR USER_TYPE = 'CONSTRACT_07')", ID];
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *nameArr = [NSMutableArray array];
    while ([result next]) {
        NSString *userID = [result stringForColumn:@"USER_ID"];
        [nameArr addObject:userID];
    }
    [db close];
    nameArr =[nameArr valueForKeyPath:@"@distinctUnionOfObjects.self"];
    return nameArr;
}

// 插入数据
-(void)chuanzhi:(NSNotification *)text {
    
    NSDictionary *imageDic = text.userInfo;
    // 流程记录ID
    self.flowRecordID = imageDic[@"ID"];
    // 附件信息
    self.fileinfos = imageDic[@"ARRAY"];
    
    if (self.problemType == 0) {
        [LSUtility showToastWithMessage:@"请选择问题类型" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if ([self.positionCell.btnStr isEqual:@"请选择位置"]) {
        [LSUtility showToastWithMessage:@"请选择位置" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if (self.sendType == 0) {
        [LSUtility showToastWithMessage:@"请选择发送形式" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if (self.addDuB && (!self.jlIDStr || [self.jlIDStr isEqualToString:@""])) {
        [LSUtility showToastWithMessage:@"请选择负责人" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if (!self.processTitle || [self.processTitle isEqualToString:@""]) {
        [LSUtility showToastWithMessage:@"请填写标题" inView:self.navigationController.view Andtime:1.];
        return;
    }
    
    if (!self.processContent || [self.processContent isEqualToString:@""]) {
        [LSUtility showToastWithMessage:@"请填写描述" inView:self.navigationController.view Andtime:1.];
        return;
    }
    
    if (!self.processRequired || [self.processRequired isEqualToString:@""]) {
        [LSUtility showToastWithMessage:@"请填写要求" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if (!self.endTime || [self.endTime isEqualToString:@""]) {
        [LSUtility showToastWithMessage:@"请选择时限" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if (!self.level || [self.level isEqualToString:@""]) {
        [LSUtility showToastWithMessage:@"请选择严重度" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if ((!self.unitID || [self.unitID isEqualToString:@""]) && !(self.addDuB && self.sendType == SEND_TYPE_NULL)) {
        [LSUtility showToastWithMessage:@"请选择整改单位" inView:self.navigationController.view Andtime:1.];
        return;
    }
    if ((!self.userID || [self.userID isEqualToString:@""]) && !(self.addDuB && self.sendType == SEND_TYPE_NULL)) {
        [LSUtility showToastWithMessage:@"请选择整改人" inView:self.navigationController.view Andtime:1.];
        return;
    }
    
    //编辑操作
    if(self.edit){
        [self updateProcess];
        [self updateProcessRecord];
    }
    //新增操作
    else{
        // 流程表
        [self addProcessData];
        // 流程记录表
        [self addProcessRecordData];
        
        if (self.addGJ) {
            // 刷新跟进列表
            [[NSNotificationCenter defaultCenter] postNotificationName:@"jlGenJin" object:nil] ;
            
        } else if(self.addDuB) {
            
            if (self.itemBlock) {
                // 发送督办单成功回调
                self.itemBlock(self.processID);
            }
            // 刷新督办列表
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshDuBList" object:nil];
        }
    }
    if(self.edit){
        if (self.addGJ) {
            // 刷新跟进列表
            [[NSNotificationCenter defaultCenter] postNotificationName:@"jlGenJin" object:nil] ;
        }
        if(self.addDuB){
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshDuBList" object:nil];
        }
    }
    
    //附件上传
    //    DataSynchronization  *sync=[[DataSynchronization alloc]init];
    //    [sync uploadAttachsBackground];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 新增流程表
- (void)addProcessData {
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *dbhelper=[DBHelper help];
    // 生成流程ID
    self.processID = [NSUUID UUID].UUIDString;
    // 流程类型
    NSString *procesType;
    // 流程负责人
    NSString *masterStr;
    // 流程状态
    NSString *processStage;
    // 流程审阅人
    NSString *reviewUser;
    if (self.addGJ) {
        procesType = [NSString stringWithFormat:@"%d", PROCESS_TYPE_FOLLOW];
        masterStr = DEF_PERSISTENT_GET_OBJECT(@"userid");
        processStage = [NSString stringWithFormat:@"%d", PROCESS_STAGE_REVIEW];
        reviewUser = @"";
        
    } else if (self.addDuB) {
        procesType = [NSString stringWithFormat:@"%d", PROCESS_TYPE_SUPERVISE];
        masterStr = self.jlIDStr;
        //如果发送状态为无 为待复查 其他为待执行
        if (self.sendType == 4) {
            processStage = [NSString stringWithFormat:@"%d", PROCESS_STAGE_CHECK];
        }else{
            processStage = [NSString stringWithFormat:@"%d", PROCESS_STAGE_DOING];
        }
        reviewUser = DEF_PERSISTENT_GET_OBJECT(@"userid");
    }
    // extendINFO
    NSDictionary *mapDic = @{};
    NSMutableDictionary *extendDic = [NSMutableDictionary dictionary];
    [extendDic setObject:mapDic forKey:@"map"];
    
    if (self.positionwStr) {
        NSDictionary *dic = [LSUtility dictionaryWithJsonString:self.positionwStr];
        NSString *str = [NSString stringWithFormat:@"%@ %@",dic[@"address"],dic[@"leftDistanceStr"]];
        [extendDic setObject:str forKey:@"address"];
    }
    NSData *extendData = [NSJSONSerialization dataWithJSONObject:extendDic options:0 error:nil];
    NSString *extendStr = [[NSString alloc] initWithData:extendData encoding:NSUTF8StringEncoding];
    //字段及对应值
    NSDictionary *pragameDic=[NSDictionary dictionary];
    pragameDic=@{
                 @"ID":DEFISNUL(self.processID),
                 @"PRO_ID":DEF_PERSISTENT_GET_OBJECT(@"proid"),
                 @"ADDRESS_ID":DEFISNUL(self.addressID),
                 @"ADDRES_INFO":DEFISNUL(self.positionwStr),
                 @"PROCESS_TYPE":DEFISNUL(procesType),
                 @"PROCESS_STAGE":processStage,
                 @"PROCESS_PUBLISHERS":DEF_PERSISTENT_GET_OBJECT(@"userid"),
                 @"PROCESS_MASTERS":DEFISNUL(masterStr),
                 @"PROCESS_RECIFY_USERS":DEFISNUL(self.userID),
                 @"PROCESS_RECIFY_UNITS":DEFISNUL(self.unitID),
                 @"RECIFY_REVIEW_USERS":reviewUser,
                 @"BEGIN_TIME":DEFISNUL(self.sendTime),
                 @"REQUIRED_END_TIME":DEFISNUL(self.endTime),
                 @"END_TIME":@"",
                 @"PROCESS_TITLE":DEFISNUL(self.processTitle),
                 @"PROCESS_NUMBER":@"",
                 @"EXTEND_INFO":DEFISNUL(extendStr),
                 @"CREATE_TIME":DEFISNUL(self.nowTime),
                 @"CREATE_USER_NAME":DEF_PERSISTENT_GET_OBJECT(@"realname"),
                 @"CREATE_IP":[LSUtility getIPAddress],
                 @"MODIFY_USER_NAME":@"",
                 @"MODIFY_IP":@"",
                 @"MODIFY_TIME":@"",
                 @"LOCAL_MODIFY_USER_NAME":@"",
                 @"LOCAL_MODIFY_TIME":@"",
                 @"LOCAL_MODIFY_STATE":@"ADD",
                 };
    BOOL isAdd=[dbhelper DataBase:db insertKeyValues:pragameDic intoTable:PROCESS];
    if(isAdd){
        NSLog(@"流程新增成功");
    }else{
        NSLog(@"流程新增失败");
    }
}

#pragma mark - 新增流程记录表
- (void)addProcessRecordData {
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *dbhelper=[DBHelper help];
    // 流程类型
    NSString *procesType;
    // 流程状态
    NSString *processStage;
    // 流程负责人
    NSString *masterStr;
    // 流程审阅人
    NSString *reviewUser;
    if (self.addGJ) {
        procesType = [NSString stringWithFormat:@"%d", PROCESS_TYPE_FOLLOW];
        processStage = [NSString stringWithFormat:@"%d", PROCESS_STAGE_REVIEW];
        masterStr = DEF_PERSISTENT_GET_OBJECT(@"userid");
        reviewUser = @"";
        
    } else if (self.addDuB) {
        procesType = [NSString stringWithFormat:@"%d", PROCESS_TYPE_SUPERVISE];
        if (self.sendType == 4) {
            processStage = [NSString stringWithFormat:@"%d", PROCESS_STAGE_CHECK];
        }else{
            processStage = [NSString stringWithFormat:@"%d", PROCESS_STAGE_DOING];
        }
        masterStr = self.jlIDStr;
        reviewUser = DEF_PERSISTENT_GET_OBJECT(@"userid");
    }
    //字段及对应值
    NSDictionary *pragameDic=[NSDictionary dictionary];
    pragameDic=@{
                 @"ID":DEFISNUL(self.flowRecordID),
                 @"PRO_ID":DEF_PERSISTENT_GET_OBJECT(@"proid"),
                 @"PROCESS_ID":DEFISNUL(self.processID),
                 @"WEATHER":@"",
                 @"ADDRESS_ID":DEFISNUL(self.addressID),
                 @"ADDRES_INFO":DEFISNUL(self.positionwStr),
                 @"PROCESS_TYPE":DEFISNUL(procesType),
                 @"PROCESS_STAGE":DEFISNUL(processStage),
                 @"RECORD_USER":DEF_PERSISTENT_GET_OBJECT(@"userid"),
                 @"PROBLEM_TYPE":[NSString stringWithFormat:@"%d", self.problemType],
                 @"RECORD_MASTER":DEFISNUL(masterStr),
                 @"RECIFY_USER":DEFISNUL(self.userID),
                 @"RECIFY_UNIT":DEFISNUL(self.unitID),
                 @"SEVERITY":DEFISNUL(self.level),
                 //三期bug解决 endtime为@""新增为空
                 @"END_TIME":@"",
                 //三期bug解决 endtime为@""新增为空
                 @"SEND_TIME":DEFISNUL(self.sendTime),
                 @"SEND_TYPE":[NSString stringWithFormat:@"%d", self.sendType],
                 @"PROCESS_TITLE":DEFISNUL(self.processTitle),
                 @"PROCESS_CONTENT":DEFISNUL(self.processContent),
                 @"PROCESS_REQUIRED":DEFISNUL(self.processRequired),
                 @"PROCESS_DESC":@"",
                 @"SUBMITTORS":DEF_PERSISTENT_GET_OBJECT(@"userid"),
                 @"PUT_USER":DEF_PERSISTENT_GET_OBJECT(@"userid"),
                 @"REVIEW_USER":DEF_PERSISTENT_GET_OBJECT(@"userid"),
                 @"REVIEW_OPINION":@"",
                 @"REVIEW_STATE":[NSString stringWithFormat:@"%d", REVIEW_STATE_UNPASS],
                 @"FILEINFOS":DEFISNUL(self.fileinfos),
                 @"EXTEND_INFO":@"",
                 @"CREATE_USER_NAME":DEF_PERSISTENT_GET_OBJECT(@"userid"),
                 @"CREATE_TIME":DEFISNUL(self.nowTime),
                 @"CREATE_IP":[LSUtility getIPAddress],
                 @"MODIFY_USER_NAME":@"",
                 @"MODIFY_IP":@"",
                 @"MODIFY_TIME":@"",
                 @"LOCAL_MODIFY_USER_NAME":@"",
                 @"LOCAL_MODIFY_TIME":@"",
                 @"LOCAL_MODIFY_STATE":@"ADD",
                 };
    BOOL isAdd=[dbhelper DataBase:db insertKeyValues:pragameDic intoTable:PROCESS_RECORD];
    if(isAdd){
        NSLog(@"流程记录新增成功");
        
    }else{
        NSLog(@"流程记录新增失败");
    }
}

// 修改流程表
- (void)updateProcess {
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *dbhelper=[DBHelper help];
    
    // 负责人编辑
    NSString *masterStr;
    if (self.addGJ) {
        masterStr = DEF_PERSISTENT_GET_OBJECT(@"userid");
        
    } else if (self.addDuB) {
        masterStr = self.jlIDStr;
    }
    // extendINFO
    NSDictionary *mapDic = @{};
    NSMutableDictionary *extendDic = [NSMutableDictionary dictionary];
    [extendDic setObject:mapDic forKey:@"map"];
    
    if (self.positionwStr) {
        NSDictionary *dic = [LSUtility dictionaryWithJsonString:self.positionwStr];
        NSString *str = [NSString stringWithFormat:@"%@ %@",dic[@"address"],dic[@"leftDistanceStr"]];
        [extendDic setObject:str forKey:@"address"];
    }
    NSData *extendData = [NSJSONSerialization dataWithJSONObject:extendDic options:0 error:nil];
    NSString *extendStr = [[NSString alloc] initWithData:extendData encoding:NSUTF8StringEncoding];
    
    //字段及对应值
    NSDictionary *pragameDic=[NSDictionary dictionary];
    pragameDic=@{
                 @"ADDRESS_ID":DEFISNUL(self.addressID),
                 @"ADDRES_INFO":DEFISNUL(self.positionwStr),
                 @"PROCESS_RECIFY_USERS":DEFISNUL(self.userID),
                 @"PROCESS_RECIFY_UNITS":DEFISNUL(self.unitID),
                 @"BEGIN_TIME":DEFISNUL(self.sendTime),
                 @"PROCESS_MASTERS":DEFISNUL(masterStr),
                 @"REQUIRED_END_TIME":DEFISNUL(self.endTime),
                 @"PROCESS_TITLE":DEFISNUL(self.processTitle),
                 @"EXTEND_INFO":DEFISNUL(extendStr),
                 @"CREATE_TIME":DEFISNUL(self.nowTime),
                 @"MODIFY_IP":[LSUtility getIPAddress],
                 @"MODIFY_TIME":DEFISNUL(self.nowTime),
                 };
    [dbhelper DataBase:db updateTable:PROCESS setKeyValues:pragameDic whereCondition:@{@"id":self.superProcessId}];
}

// 修改流程记录表
-(void)updateProcessRecord {
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    DBHelper *dbhelper=[DBHelper help];
    
    // 负责人编辑
    NSString *masterStr;
    if (self.addGJ) {
        masterStr = DEF_PERSISTENT_GET_OBJECT(@"userid");
    } else if (self.addDuB) {
        masterStr = self.jlIDStr;
    }
    //字段及对应值
    NSDictionary *pragameDic=[NSDictionary dictionary];
    pragameDic=@{
                 @"ADDRESS_ID":DEFISNUL(self.addressID),
                 @"ADDRES_INFO":DEFISNUL(self.positionwStr),
                 @"PROBLEM_TYPE":[NSString stringWithFormat:@"%d", self.problemType],
                 @"RECIFY_USER":DEFISNUL(self.userID),
                 @"RECIFY_UNIT":DEFISNUL(self.unitID),
                 @"SEVERITY":DEFISNUL(self.level),
                 @"RECORD_MASTER":DEFISNUL(masterStr),
                 @"SEND_TIME":DEFISNUL(self.sendTime),
                 @"SEND_TYPE":[NSString stringWithFormat:@"%d", self.sendType],
                 @"PROCESS_TITLE":DEFISNUL(self.processTitle),
                 @"PROCESS_CONTENT":DEFISNUL(self.processContent),
                 @"PROCESS_REQUIRED":DEFISNUL(self.processRequired),
                 @"FILEINFOS":DEFISNUL(self.fileinfos),
                 @"MODIFY_TIME":DEFISNUL(self.nowTime),
                 @"LOCAL_MODIFY_STATE":@"ADD",
                 };
    [dbhelper DataBase:db updateTable:PROCESS_RECORD setKeyValues:pragameDic whereCondition:@{@"id":self.recordModel.ID}];
}



@end


